#ifndef __CHESSBOARD__
#define __CHESSBOARD__
#include <iostream>
#include <stdio.h>
#include "cursor.h"
#include "checkerboard.h"
#define X 15
#define Y 15
using namespace std;
class ChessBoard{
	public:
		~ChessBoard(){
			printf("\033[%d;%dH",MAX_Y+1,MIN_X);
		}
		class GC{
			public:
			~GC(){
				delete chessBoard;
			}	
		};
		static ChessBoard *getChessBoard(){
			if(!chessBoard){
				chessBoard=new ChessBoard();
			}
			return chessBoard;
		}
		void show(){
			printf("\033[%d;%dH",MIN_Y,MIN_X);
			FILE *file;
			file=fopen("ChessBoard.txt","r");
			if(!file){
				perror("fopen is failed");
				return;
			}
			char buff[1024];
			while(fgets(buff,sizeof(buff),file))
			{
				printf("%s",buff);	
			}
			fclose(file);
		}
		int getLine(){return line;}
		int getCol(){return col;}
		void chessPlay(Checkerboard *checkerboard){
			line=checkerboard->getY()/Y_SPLIT;
			col=checkerboard->getX()/X_SPLIT;
			checkerboards[line][col]=checkerboard;
			checkerboard->show();
		}
		bool ischeck(int x,int y){
			if(x>MAX_X || x<MIN_X){
				return false;
			}
			if(y>MAX_Y || y<MIN_Y){
				return false;
			}
			int col=x/X_SPLIT;
			int line=y/Y_SPLIT;
			
			return checkerboards[line][col]==nullptr?true:false;
		}
		int getCursorval(const string &color,int line,int col){
			if(col>X || col<0){
				return 0;
			}
			if(line>Y || line<0){
				return 0;
			}
			Checkerboard *checkerboard=checkerboards[line][col];
			if(checkerboard==nullptr){
				return 0;
			}
			if(checkerboard->getColor()==color){
				return 1;
			}else{
				return 0;
			}
			return 0;
		}
	private:
		Checkerboard *checkerboards[X][Y];
		int line;
		int col;
	 	static ChessBoard *chessBoard;
		ChessBoard(){
			for(int i=0;i<X;i++){
				for(int j=0;j<Y;j++){
					checkerboards[i][j]=nullptr;
				}
			}
		}
		static GC gc;

};
ChessBoard *ChessBoard::chessBoard=nullptr;
ChessBoard::GC ChessBoard::gc;
#endif
